package os.kai.aiu;

import org.apache.commons.lang3.tuple.ImmutablePair;
import org.apache.commons.lang3.tuple.Pair;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class MathUtil {

    private static Map<Character,Integer> CHINESE_DIGIT_MAP = new HashMap<>();
    static {
        for(Pair<Character,Integer> kv : Arrays.asList(
                ImmutablePair.of('一',1),
                ImmutablePair.of('二',2),
                ImmutablePair.of('三',3),
                ImmutablePair.of('四',4),
                ImmutablePair.of('五',5),
                ImmutablePair.of('六',6),
                ImmutablePair.of('七',7),
                ImmutablePair.of('八',8),
                ImmutablePair.of('九',9),
                ImmutablePair.of('〇',0),
                ImmutablePair.of('壹',1),
                ImmutablePair.of('贰',2),
                ImmutablePair.of('叁',3),
                ImmutablePair.of('肆',4),
                ImmutablePair.of('伍',5),
                ImmutablePair.of('陆',6),
                ImmutablePair.of('柒',7),
                ImmutablePair.of('捌',8),
                ImmutablePair.of('玖',9),
                ImmutablePair.of('零',0)
        )){
            CHINESE_DIGIT_MAP.put(kv.getKey(),kv.getValue());
        }
    }
    private static Map<Character,Integer> CHINESE_UNIT_MAP = new HashMap<>();
    static {
        for(Pair<Character,Integer> kv : Arrays.asList(
                ImmutablePair.of('十',10),
                ImmutablePair.of('百',100),
                ImmutablePair.of('千',1000),
                ImmutablePair.of('拾',10),
                ImmutablePair.of('佰',100),
                ImmutablePair.of('仟',1000)
        )){
            CHINESE_UNIT_MAP.put(kv.getKey(),kv.getValue());
        }
    }
    public static int parseChineseNumberUnder9999(String cn, boolean useDefUnit) {
        int end = cn.length();
        if(end==0){
            return 0;
        }
        int index = 0;
        int r = 0;
        int defUnit = 0;
        while(index<end){
            char c = cn.charAt(index);
            Integer digit = CHINESE_DIGIT_MAP.get(c);
            int dv;
            if(digit!=null){
                //start with digit, use the digit and move to next
                dv = digit;
                index++;
            }
            else {
                //start may be with unit, value should be one unit
                if(CHINESE_UNIT_MAP.containsKey(c)){
                    dv = 1;
                }
                else {
                    return 0;
                }
            }
            int uv;
            if(index<end){
                c = cn.charAt(index);
                Integer unit = CHINESE_UNIT_MAP.get(c);
                if(unit!=null){
                    //find unit, use unit, and move to next digit
                    uv = unit;
                    defUnit = unit/10;
                    index++;
                }
                else {
                    //not a unit, use def unit and reset def unit, keep current digit
                    uv = useDefUnit?defUnit:0;
                    defUnit = 0;
                }
            }
            else {
                //last digit, use def unit, or only one digit
                uv = !useDefUnit||defUnit==0?1:defUnit;
                defUnit = 0;
            }
            r += dv*uv;
        }
        return r;
    }
    public static int parseChineseNumberUnder9999(String cn) {
        return parseChineseNumberUnder9999(cn,false);
    }
}
